package com.gpframework.schedule;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.gpframework.common.core.redis.RedisCache;
import com.gpframework.module.common.service.ICommonPhoneMsgTemplateService;
import com.gpframework.module.module.mapper.BrandStoreMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * gpframework
 * ==================================================================
 * CopyRight © 2017-2099 广州宏略信息科技有限公司
 * 官网地址：http://www.greatplan.com.cn
 * <p>
 * ------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下
 * 对本程序代码进行修改和使用；不允许对本程序代码以任何目的的再发布。
 * ==================================================================
 *
 * @ClassName ScheduleTask
 * @Author gpframework
 * @Date 2020-12-30
 * @Version 1.0.0
 * EnableScheduling : 1.开启定时任务
 * EnableAsync :2.开启多线程
 */
@Component
@EnableScheduling
@EnableAsync
@Slf4j
public class ScheduleTask {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BrandStoreMapper brandStoreMapper;
    @Autowired
    private ICommonPhoneMsgTemplateService iCommonPhoneMsgTemplateService;

    private static final String SEND_CONTENT = "%s先生/小姐，您的%s于%s到期。请及时续费";


    /**
     * 开始时间
     */
    private static Long startTime;
    /**
     * 结束时间
     */
    private static Long endTime;

    /**
     * 毫秒
     */
    private static final int millisecond = 1000;

    /**
     * 开始输出定时任务日志
     */
    private void startOutputTaskLog(String msg) {
        log.info("----------{}----------", msg);
        String str = "";
        startTime = System.currentTimeMillis();
        str += ("FREEMEMORY : " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + " M");
        str += ("\n - TOTALMEMORY : " + Runtime.getRuntime().totalMemory() / 1024 / 1024 + " M");
        str += ("\n - MAXMEMORY : " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " M");
        log.info(str);
    }

    /**
     * 结束输出定时任务日志
     */
    private void endOutputTaskLog(String msg) {
        endTime = System.currentTimeMillis();
        String str = "";
        str += ("执行时长：{" + (endTime - startTime) / millisecond + "} 秒");
        str += ("\n - FREEMEMORY : " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + " M");
        str += ("\n - TOTALMEMORY : " + Runtime.getRuntime().totalMemory() / 1024 / 1024 + " M");
        str += ("\n - MAXMEMORY : " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " M");
        log.info(str);
        log.info("----------{}----------", msg);
    }

    /**
     * 门店准备到期短信提示
     * 每天早上
     */
/*    @Async
    @Transactional
    //每天下午2点执行
    @Scheduled(cron = "0 0 0/14 * * ?")*/
    //五秒执行一次
//    @Scheduled(cron = "*/10 * * * * ?")
/*    public void sendStoresPreparationExpireMsg() {
        log.debug("----------执行门店到期通知----------");
        BrandStoreReq brandStoreReq = new BrandStoreReq();
        brandStoreReq.setAuditStatus(CommonStatus.THROUGH.getKey().toString());
        brandStoreReq.setPreparationExpirePromptDay(BrandConstants.STORES_PREPARATION_EXPIRE_PROMPT_DAY);
        List<BrandStoreVo> storeVoList = brandStoreMapper.getStoresPreparationExpireList(brandStoreReq);
        if (StringUtils.isEmpty(storeVoList) || storeVoList.size() <= 0) {
            return;
        }
        //发送通知
        BrandSendSmsReq brandSendSmsReq = new BrandSendSmsReq();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (BrandStoreVo storeVo : storeVoList) {
            logger.info("门店信息----》{}《----", storeVo);
            if (StringUtils.isEmpty(storeVo.getStoreMobile())) {
                continue;
            }
            brandSendSmsReq.setBrandId(BrandConstants.DEFAULT_BRAND_ID);
            brandSendSmsReq.setStoreId(BrandConstants.DEFAULT_BRAND_ID);
            brandSendSmsReq.setPhonenumber(storeVo.getStoreMobile());
            brandSendSmsReq.setBusinessType(PhoneMsgTypeEnum.STORES_PREPARATION_EXPIRE.getKey());
            brandSendSmsReq.setBusinessName(PhoneMsgTypeEnum.STORES_PREPARATION_EXPIRE.getName());
            String format = sdf.format(storeVo.getEffTimeEnd());
            String word = String.format(SEND_CONTENT, storeVo.getLeaderName(), storeVo.getStoreName(), format);
            brandSendSmsReq.setSendContent(word);
            iCommonPhoneMsgTemplateService.sendSms(brandSendSmsReq);
        }
    }*/



}
